<template>
	<view class="container">
		<view class="step" v-show="intActive==0">
			<view class="c-input">
				<input type="number" @input="onLogMobile" v-model="logMobile" placeholder="请输入手机号" />
			</view>
			<view class="c-input c-pos">
				<input type="number" @input="onLogRnd" v-model="logRnd" placeholder="请输入右侧随机码" />
				<button type="default" size="mini" class="c-input-btn c-regcode" disabled="true">{{rdnNum}}</button>
			</view>
			<view class="c-input">
				<button type="primary" :disabled="blnDisabled_code" class="c-input-btn" @click="onGetEditCode">获取短信验证码</button>
			</view>
		</view>
		<view class="step" v-show="intActive==1">
			<view class="c-input">
				<input type="text" @input="onLogCode" v-model="logCode" placeholder="请输入短信验证码" />
			</view>
			<view class="c-input">
				<input type="password" @input="onNewPassWord" v-model="newPassWord" placeholder="请输入重置新密码" />
			</view>
			<view class="c-input">
				<button type="primary" :disabled="blnDisabled_edit" class="c-input-btn" @click="onSaveEditPassWord">确定并保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	let pageApp = getApp();
	export default {
		data() {
			return {
				logMobile: "",
				logRnd: "",
				logCode: "",
				newPassWord: "",
				rdnNum: this.getRndCode(4),
				intActive: 0,
				blnDisabled_code: true,
				blnDisabled_edit: true
			}
		},
		methods: {
			onLogMobile(e) {
				this.blnDisabled_code = this.checkIsEmpty(this.logMobile, this.logRnd);
			},
			onLogRnd(e) {
				this.blnDisabled_code = this.checkIsEmpty(this.logMobile, this.logRnd);
			},
			onLogCode(e) {
				this.blnDisabled_edit = this.checkIsEmpty(this.logCode, this.newPassWord);
			},
			onNewPassWord(e) {
				this.blnDisabled_edit = this.checkIsEmpty(this.logCode, this.newPassWord);
			},
			onGetEditCode(e) {
				// 基础验证
				if (this.checkMobileCode(this.logMobile)) {
					uni.showToast({
						icon: "none",
						title: "手机号格式有误！"
					})
				} else {
					if (this.logRnd.trim().length != 4) {
						uni.showToast({
							icon: "none",
							title: "随机码长度有误！"
						})
					} else {
						if (this.rdnNum == this.logRnd) {
							this.blnDisabled_code = true;
							uni.showToast({
								icon: "none",
								title: "验证码正在发送中..."
							})
							// 准备请求数据
							let apiData = {
								url: pageApp.globalData.url + '/ModifyPwdMsgCheck',
								data: {
									"UserIdOrMobile": this.logMobile
								},
								method: "POST",
								success: (d) => {
									if (d.data.code == "001") {
										uni.showToast({
											icon: "none",
											title: "验证码已发送",
											success: () => {
												this.intActive = 1;
												uni.setNavigationBarTitle({
													title: "设置新密码-密码重置"
												})
											}
										})
									} else {
										uni.showToast({
											icon: "none",
											title: d.data.msg
										})
									}
								}
							}
							// 发送数据请求
							this.vjAjaxData(apiData);
						} else {
							uni.showToast({
								icon: "none",
								title: "随机码不正确！"
							})
						}
					}
				}
			},
			onSaveEditPassWord(e) {
				// 基础验证
				if (this.logCode.trim().length != 6) {
					uni.showToast({
						icon: "none",
						title: "验证码长度有误！"
					})
				} else {
					if (this.newPassWord.trim().length >= 6) {
						// 准备请求数据
						let apiData = {
							url: pageApp.globalData.url + '/ModifyPwd',
							data: {
								"UserIdOrMobile": this.logMobile,
								"PasswordSalt": this.newPassWord,
								"CheckCode": this.logCode
							},
							method: "POST",
							success: (d) => {
								if (d.data.code == "001") {
									uni.showToast({
										title: "密码修改成功！",
										success: () => {
											// 返回登录页
											setTimeout(() => {
												uni.navigateTo({
													url: "./login/login"
												})
											}, 3000)
										}
									})
								} else {
									uni.showToast({
										icon: "none",
										title: d.data.msg
									})
								}
							}
						}
						// 发送数据请求
						this.vjAjaxData(apiData);
					} else {
						uni.showToast({
							icon: "none",
							title: "密码必须大于6位"
						})
					}
				}
			}
		}
	}
</script>

<style>

</style>
